﻿using System;
using System.Collections;
using System.Windows.Forms;
using RMS.common;
using RMS.Common;

namespace RMS
{
    public partial class frmMain : ICommonForm
    {
        public int count;
        FormState formState = new FormState();
        

        public frmMain()
        {
            InitializeComponent();
            
            if(Constants.FullScreen()) formState.Maximize(this);
        }


        private void listToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var frmList = new frmListStock() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };
            CloseMdiChildForm();
            frmList.MdiParent = this;
            frmList.Show();
        }

        private void CloseMdiChildForm()
        {           
           foreach (var openForm in this.MdiChildren)
            {                
              openForm.Close();
            }
        }

        private void toolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
        {
            if (e.Button == tbbInv)
            {
                Form frmInv = new frmSellInventory() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };
                CloseMdiChildForm();
                frmInv.MdiParent = this;
                frmInv.Show();
            }
            else if (e.Button == tbbInputInv)
            {
                var frm = new frmInventory() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };
                CloseMdiChildForm();
                frm.MdiParent = this;
                frm.Show();
            }
            else if (e.Button == tbbDraft)
            {
                var frm = new frmListDraftInvoices() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };
                CloseMdiChildForm();
                frm.MdiParent = this;
                frm.Show();
            }
            else if (e.Button == tbbLogOff)
            {
                //if (MessageBox.Show("Bạn có chắc chắn muốn đăng xuất ?", "Chú ý", MessageBoxButtons.YesNo) != DialogResult.Yes) return;
                CloseMdiChildForm();
                EnableAllControl(false);
                menuToolStripMenuItem.Visible = báoCáoToolStripMenuItem.Visible = tbbInputInv.Enabled =
                    TaiKhoanToolStripMenuItem.Visible =
                càiĐặtToolStripMenuItem.Visible = false;
                regPanel.Visible = false;
                panelLogin.Visible = panelLogin.Enabled = true;
                foreach (Control ctrl in this.panelLogin.Controls)
                {
                    ctrl.Enabled = true;
                } 
            }
            else if (e.Button == tbbExit)
            {
                Application.Exit();
            }

          //  KeyPreview = true;
        }


        private void sảnPhẩmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new frmSellInventory() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };
            CloseMdiChildForm();
            frm.MdiParent = this;
            frm.Show();
        }

        private void doanhThuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseMdiChildForm();
            Form frm = new frmStatistic() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };
            frm.MdiParent = this;            
            frm.Show();
        }

        private void frmMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                CloseMdiChildForm();

                frmSellInventory frm = new frmSellInventory() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };                
                frm.MdiParent = this;
                frm.Show();
                
            }

            else if (e.KeyCode == Keys.F3)
            {
                CloseMdiChildForm();
                Form frm = new frmInventory() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };                
                frm.MdiParent = this;
                frm.Show();
            }

            else if (e.KeyCode == Keys.F4)
            {
                CloseMdiChildForm();
                var frm = new frmListDraftInvoices() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };                
                frm.MdiParent = this;
                frm.Show();
            }

            else if (e.KeyCode == Keys.F5)
            {
                đăngXuấtToolStripMenuItem_Click(sender, e);
            }

            else if (e.KeyCode == Keys.F6)
            {
                Application.Exit();
            }

            Form activeForm = ActiveMdiChild;
            if (activeForm != null && activeForm.Name == "frmSellInventory")
            {
                if (e.KeyCode == Keys.F9 || e.KeyCode == Keys.F10)
                {
                    frmSellInventory formSell = (frmSellInventory)activeForm;
                    formSell.frmSellInventory_KeyDown(sender, e);
                }
            }

            


            KeyPreview = true;

        }
        private void thayĐổiMậtKhẩuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new frmManagePassword() { WindowState = WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };
            CloseMdiChildForm();
            frm.MdiParent = this;
            frm.Show();
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {            
            if (MessageBox.Show(Constants.Msg.Exit, "Chú ý",
               MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {                
                e.Cancel = true;
            }
        }

        private void thôngTinToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseMdiChildForm();
            Form frm = new frmProviderInfo() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };                        
            frm.MdiParent = this;
            frm.Show();
        }

        private void kháchHàngToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            CloseMdiChildForm();
            Form frm = new frmCustomersList() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };
            frm.MdiParent = this;
            frm.Show();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            TaiKhoanToolStripMenuItem.Visible = false;
            if(CheckInfo())
            {
                EnableControl(true);
                panelLogin.Enabled = true;
                panelLogin.Visible = true;
                panelLogin.BringToFront();
            }
            else
            {
                EnableControl(true);
                regPanel.Visible = true;
                panelLogin.Enabled = panelLogin.Visible = false;
            }
        }

        private void EnableControl(bool enable)
        {
            foreach (Control control in this.Controls)
            {
                if (control.Name != "regPanel")
                    control.Enabled = !enable;
            }
            TaiKhoanToolStripMenuItem.Visible = enable;
            regPanel.Visible = !enable;
            if (regPanel.Visible)
            foreach (Control regCtrl in this.regPanel.Controls)
            {
                regCtrl.Enabled = !enable;
            }
        }

        private void EnableAllControl(bool enable)
        {
            foreach (Control control in this.Controls)
            {
                if (control.Name != "regPanel")
                    control.Enabled = enable;
            }   
        }
        private bool CheckInfo()
        {
            lblWarning.Text = "";

            ModifyRegistry registry = new ModifyRegistry();

            //Check is trial or full
            var lisenceExpire = registry.Read(Constants.RegistryLisenceExpire);
            if (string.IsNullOrEmpty(lisenceExpire))//Expired
            {
                lblWarning.Text = "Phiên bản hiện tại chưa đăng kí bản quyền !";
            }
            else
            {
                DateTime expireDate = new DateTime();
                if (DateTime.TryParse(lisenceExpire, out expireDate))
                {
                    if (expireDate < DateTime.Now)
                    {
                        lblWarning.Text = "Phần mềm đã hết hạn sử dụng !";
                    }
                    else
                    {
                        return true;
                    }
                }
                else
                {
                    lblWarning.Text = "Phiên bản hiện tại chưa đăng kí bản quyền !";
                }
            }
            return false;
        }

        private void btnReg_Click(object sender, EventArgs e)
        {
            string user = txtUserName.Text.Trim();
            if (string.IsNullOrEmpty(user))
            {
                lblMsg.Text = "Hãy nhập tên người dùng.";
                lblMsg.Visible = true;
                txtUserName.Focus();
                return;
            }

            string code = txtCode.Text.Trim();
            if (string.IsNullOrEmpty(code))
            {
                lblMsg.Text = "Hãy nhập mã đăng ký.";
                lblMsg.Visible = true;
                txtCode.Focus();
                return;
            }

            if (Utilities.Lisence(user) == code)
            {
                //Check current key
                ModifyRegistry registry = new ModifyRegistry();

                if (registry.Read(Constants.RegistryLisenceKey) == code)
                {
                    MessageBox.Show("Mã này đã được sử dụng, vui lòng nhập mã bản quyền khác.");
                }
                else
                {
                    registry.Write(Constants.RegistryLisenceName, user);
                    registry.Write(Constants.RegistryLisenceKey, code);

                    if (user.ToLower().Contains("demo") || user.ToLower().Contains("trial"))
                    {
                        registry.Write(Constants.RegistryLisenceExpire, DateTime.Now.AddMonths(1));
                    }
                    else
                    {
                        registry.Write(Constants.RegistryLisenceExpire, DateTime.Now.AddYears(99));
                    }

                    MessageBox.Show("Cảm ơn ! Bạn đã đăng kí bản quyền thành công !");
                    this.DialogResult = DialogResult.OK;
                    EnableControl(true);
                    panelLogin.Enabled = panelLogin.Visible = true;
                }
            }
            else
            {
                count = count + 1;
                lblMsg.Text = "Mã đăng kí không đúng !";
                lblMsg.Visible = true;
                if (count > 3)
                {
                    MessageBox.Show("Bạn đã nhập sai mã đăng kí quá 3 lần !");
                    this.DialogResult = DialogResult.Cancel;
                    this.Close();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void linkSupport_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var about = new frmProviderInfo() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };
            about.ShowDialog();
        }

        private void txtUserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnReg_Click(sender, e);
            }
        }

        private void txtCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnReg_Click(sender, e);
            }

        }

        private void s(object sender, LinkLabelLinkClickedEventArgs e)
        {

        }

        private void btnNV_Click(object sender, EventArgs e)
        {
            panelLogin.Visible = false;
            menuToolStripMenuItem.Visible = báoCáoToolStripMenuItem.Visible =
                tbbInputInv.Visible = thayĐổiMậtKhẩuToolStripMenuItem1.Visible=
            càiĐặtToolStripMenuItem.Visible = false;
            TaiKhoanToolStripMenuItem.Visible = true;
            EnableAllControl(true);
        }

        private void btnQL_Click(object sender, EventArgs e)
        {
            var location = new DialogCheckAdmin() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };
            if (location.ShowDialog() == DialogResult.OK)
            {                
                EnableControl(true);
                panelLogin.Visible = false;
                menuToolStripMenuItem.Visible = báoCáoToolStripMenuItem.Visible =
                    tbbInputInv.Enabled = thayĐổiMậtKhẩuToolStripMenuItem1.Visible =
                    càiĐặtToolStripMenuItem.Visible = tbbInputInv.Visible = true;
                EnableAllControl(true);
            }
        }

        private void đăngXuấtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseMdiChildForm();
            EnableAllControl(false);
            menuToolStripMenuItem.Visible = báoCáoToolStripMenuItem.Visible = tbbInputInv.Visible =
                TaiKhoanToolStripMenuItem.Visible=
            càiĐặtToolStripMenuItem.Visible = false;
            regPanel.Visible = false;
            panelLogin.Visible = panelLogin.Enabled = true;
            foreach (Control ctrl in this.panelLogin.Controls)
            {
                ctrl.Enabled = true;
            }
        }

        private void thayĐổiMậtKhẩuToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form frm = new frmManagePassword() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };
            CloseMdiChildForm();
            frm.MdiParent = this;
            frm.Show();
        }

        private void càiĐặtThẻVIPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new frmDiscountVIPMng() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };
            CloseMdiChildForm();
            frm.MdiParent = this;            
            frm.Show();
        }

        private void quảnLýHọcViênToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            CloseMdiChildForm();
            //Form frm = new frmApprentice() { MdiParent = this };
            //frm.WindowState = FormWindowState.Maximized;
            //frm.Show();
        }

        private void càiĐặtBánHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseMdiChildForm();
            Form frm = new frmEnableSalePriceMng() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };
            frm.MdiParent = this;
            frm.Show();
        }

        private void maVachToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseMdiChildForm();
            Form frm = new frmBarCodePrint() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };
            frm.MdiParent = this;
            frm.Show();
        }

        private void nhapHangToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseMdiChildForm();
            Form frm = new frmImportProduct() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen  };
            frm.MdiParent = this;
            frm.Show();
        }

        private void saoLuuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseMdiChildForm();
            Form frm = new frmBackup() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };
            frm.MdiParent = this;
            frm.Show();
        }

        private void thietLapLaiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseMdiChildForm();
            Form frm = new frmReset() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };
            frm.MdiParent = this;
            frm.Show();
        }

        private void thongTinShopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseMdiChildForm();
            Form frm = new frmShopInfo() { WindowState = FormWindowState.Normal, StartPosition = FormStartPosition.CenterScreen };
            frm.MdiParent = this;
            frm.Show();
        } 
    }
}